package com.crm.suborgConfig.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.crm.suborgConfig.domain.BaseSuborgConfig;
import com.crm.suborgConfig.service.IBaseSuborgConfigService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分公司配置Controller
 * 
 * @author ruoyi
 * @date 2024-01-29
 */
@RestController
@RequestMapping("/crmsys/companyInfo")
public class BaseSuborgConfigController extends BaseController
{
    @Autowired
    private IBaseSuborgConfigService baseSuborgConfigService;

    /**
     * 查询分公司配置列表
     */
    @PreAuthorize("@ss.hasPermi('crmsys:companyInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BaseSuborgConfig baseSuborgConfig)
    {
        startPage();
        List<BaseSuborgConfig> list = baseSuborgConfigService.selectBaseSuborgConfigList(baseSuborgConfig);
        return getDataTable(list);
    }
    /**
     * 查询所有分公司信息
     */
    @PreAuthorize("@ss.hasPermi('crmsys:companyInfo:list')")
    @GetMapping("/getAllList")
    public TableDataInfo getAllList(BaseSuborgConfig baseSuborgConfig)
    {
        List<BaseSuborgConfig> list = baseSuborgConfigService.selectBaseSuborgConfigList(baseSuborgConfig);
        return getDataTable(list);
    }
    /**
     * 导出分公司配置列表
     */
    @PreAuthorize("@ss.hasPermi('crmsys:companyInfo:export')")
    @Log(title = "分公司配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseSuborgConfig baseSuborgConfig)
    {
        List<BaseSuborgConfig> list = baseSuborgConfigService.selectBaseSuborgConfigList(baseSuborgConfig);
        ExcelUtil<BaseSuborgConfig> util = new ExcelUtil<BaseSuborgConfig>(BaseSuborgConfig.class);
        util.exportExcel(response, list, "分公司配置数据");
    }

    /**
     * 获取分公司配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('crmsys:companyInfo:query')")
    @GetMapping(value = "/{orgId}")
    public AjaxResult getInfo(@PathVariable("orgId") Long orgId)
    {
        return success(baseSuborgConfigService.selectBaseSuborgConfigByOrgId(orgId));
    }

    /**
     * 新增分公司配置
     */
    @PreAuthorize("@ss.hasPermi('crmsys:companyInfo:add')")
    @Log(title = "分公司配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseSuborgConfig baseSuborgConfig)
    {
        return toAjax(baseSuborgConfigService.insertBaseSuborgConfig(baseSuborgConfig));
    }

    /**
     * 修改分公司配置
     */
    @PreAuthorize("@ss.hasPermi('crmsys:companyInfo:edit')")
    @Log(title = "分公司配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseSuborgConfig baseSuborgConfig)
    {
        return toAjax(baseSuborgConfigService.updateBaseSuborgConfig(baseSuborgConfig));
    }

    /**
     * 删除分公司配置
     */
    @PreAuthorize("@ss.hasPermi('crmsys:companyInfo:remove')")
    @Log(title = "分公司配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orgIds}")
    public AjaxResult remove(@PathVariable Long[] orgIds)
    {
        return toAjax(baseSuborgConfigService.deleteBaseSuborgConfigByOrgIds(orgIds));
    }
    @Anonymous
    @GetMapping("/getCity")
    public AjaxResult getCity(BaseSuborgConfig baseSuborgConfig)
    {
        List<BaseSuborgConfig> list = baseSuborgConfigService.selectBaseSuborgConfigList(baseSuborgConfig);
        List<String> citys=new ArrayList<>();
        for (BaseSuborgConfig suborgConfig : list) {
            citys.add(suborgConfig.getCity());
        }

        return AjaxResult.success(citys);
    }
}
